/**
 *
 * @author Elias la Cour
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rnavizexperimentation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/2508704 */
public class CircularFeynman extends JPanel {

    private static final int SIZE = 1024;
    private int a = SIZE / 2;
    private int b = a;
    private int r = 4 * SIZE / 5;
    private int n;
    private Font font = new Font("Serif", Font.PLAIN, 20);
    private static char seq[];

    /** @param n  the desired number of circles. */
    public CircularFeynman(int n) {
        super(true);
        this.setPreferredSize(new Dimension(SIZE, SIZE));
        this.n = n;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        //g2d.setColor(Color.black);
        //gets center of canvas
        a = getWidth() / 2;
        b = getHeight() / 2;
        int m = Math.min(a, b);
        //? rbruges til at udregne omkreds af cirklen?
        r = 4 * m / 5;
        //r2? bruges til at tegne cirkel for hvert punkt?
        //int r2 = Math.abs(m - r) / 2;
        //g2d.drawOval(a - r, b - r, 2 * r, 2 * r);
        g2d.setColor(Color.blue);

        //for rotating glyphs
        g2d.setFont(font);
        FontRenderContext frc = g2d.getFontRenderContext();
        GlyphVector gv = font.createGlyphVector(frc, seq);
        int n = gv.getNumGlyphs();
        AffineTransform at = new AffineTransform();
        AffineTransform shift = new AffineTransform();
        Point2D p;
        Double theta;
        Shape glyph;

        for (int i = 0; i < n; i++) {
            p = gv.getGlyphPosition(i);
            theta = (2 * Math.PI * i) / n;
            int x = (int) Math.round(a + r * Math.cos(theta));
            int y = (int) Math.round(b + r * Math.sin(theta));
            at.setToRotation(theta, x, y);


            glyph = gv.getGlyphOutline(i);
            glyph = at.createTransformedShape(glyph);
            g2d.fill(glyph);

            //g2d.drawChars(seq, 0, length, x, y);
            //g2d.drawChars(seq, 0, length, x, y);

        }
        

//        for (int i = 0; i < n; i++) {
//            //t = theta
//            double t = 2 * Math.PI * i / n;
//            //cos finder nyt x coord, sin finder nyt y coord
//            int x = (int) Math.round(a + r * Math.cos(t));
//            int y = (int) Math.round(b + r * Math.sin(t));
//            g2d.drawString("b", x, y);
//            //g2d.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);
//        }
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        seq = new InputHandler().smallInput().getBaseSequence();
        f.add(new CircularFeynman(seq.length));
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }
}
